package com.swt.dao.impl.S_impl;

import com.swt.dao.S_dao.AboutCourseDao;
import com.swt.domain.Aboutcourse;
import com.swt.domain.Course;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "S_aboutCourseDao")
public class AboutCourseDaoImpl implements AboutCourseDao {
    /*注入hibernateTemplate*/
    @Resource(name = "hibernateTemplate")
    private HibernateTemplate hibernateTemplate;

    @Override
    public void save(Aboutcourse aboutcourse) {
        /*保存到数据库*/
        hibernateTemplate.save(aboutcourse);
    }

    @Override
    public List<Aboutcourse> select(DetachedCriteria detachedCriteria) {
        //查询
        List<Aboutcourse> list = (List<Aboutcourse>)hibernateTemplate.findByCriteria(detachedCriteria);
        return list;
    }

    @Override
    public void updatenum(Integer aboutcourseCourseid) {
        /*先到数据库中查询出来对应的课程信息*/
        Course course = hibernateTemplate.get(Course.class, aboutcourseCourseid);
        //将数量-1
        course.setCourseRemainNum(course.getCourseRemainNum()-1);
        //更新到数据库
        hibernateTemplate.update(course);
    }
}
